<template>

	<view class="page">

		<uni-card margin='2px' style="margin-bottom: 20px; padding: 5px;">
			<view @click="openSelector">
			<mpopupHeaderItem label="申请人名称" :value="orderData.operatorName  ||'[修改]'"  height="90px" />
			<mpopupHeaderRadioItem label="受理号" height="90px" :custom-class="'custom-align'" :show-bottom-border="true"
				:custom-style="{ padding: '20rpx' }">
				<text>{{orderData.acceptNumber}} </text>
			</mpopupHeaderRadioItem>
			</view>
		</uni-card>

		<uni-card margin='2px' style="margin-bottom: 20px; padding: 5px;">
		    <uni-title type="h1" align="center" title="撤销理由"></uni-title>
		    <view class="uni-column">
		      <textarea 
		        v-model="orderData.remark" 
		        class="multi-line-input"
		        placeholder="请填写相关内容"
		        :auto-height="true"
				maxlength="10000"
		        placeholder-class="placeholder-style"
		        :show-confirm-bar="false"
		        :adjust-position="false"
		      ></textarea>
		    </view>
		  </uni-card>



		<view v-if="editStatus!='FALSE'" class="button-container">
			<view class="button-save">
				<button style="flex: 1;" type="primary" @click="submitForm('1')">保存并提交撤销审查单</button>
			</view>
			<view class="button-save">
				<button style="flex: 1;" type="primary" @click="submitForm('0')">暂存</button>
			</view>
		</view>
		
		<EnterpriseSelector tableType="2" :query="{orderType: '2', auditStatus: '1'}" type="radio"
			ref="selector" :multiple="false" @confirm="handleConfirm" @close="handleClose" />
		
	</view>

</template>

<script>
	import mpopupHeaderItem from "@/components/mpopup-header-item.vue"
	import mpopupHeaderRadioItem from "@/components/mpopup-header-radio-item.vue"
	import {
		foodProductionAdd,
		foodProductionInfo,
		foodProductionUpdate
	} from '@/api/order'
	import EnterpriseSelector from "@/components/dialog/enterprise-dialog.vue"
	export default {
		onLoad(options) {

			if (options.editStatus != null) {
				const editStatus = decodeURIComponent(options.editStatus);
				const workOrderId = decodeURIComponent(options.workOrderId);
			
				this.workOrderId=workOrderId;
				this.editStatus = options.editStatus;
					this.getfoodProductionInfo(workOrderId);
			} else {
				//上个界面的传参
				if (options.orderData) {
					const orderData = JSON.parse(decodeURIComponent(options.orderData));
					const workOrderId = decodeURIComponent(options.workOrderId);
					this.orderData.operatorName = orderData.operatorName;
					this.orderData.acceptNumber = orderData.acceptNumber;
					this.workOrderId=workOrderId;
					this.orderData.bidType = orderData.bidType;
					this.orderData.orderCategory = orderData.orderCategory;
				}
			}

		},
		components: {
EnterpriseSelector,
			mpopupHeaderRadioItem,
			mpopupHeaderItem, // 需先导入组件

		},
		computed: {

		},
		data() {
			return {
				orderData: {
					operatorName: '',
					acceptNumber: '',
					remark: '',
					createBy:'',
					workOrderId:'',
					orderCategory:'',
					bidType:''
				},
workOrderId:'',
				editStatus: 'ADD',
				valueOptions: [],
				bindType: ''
			}
		},
		methods: {
			// 打开选择器
			openSelector() {
				this.$refs.selector.open()
			},
			handleConfirm(selected) {
				this.orderData = selected
			},
			handleClose() {
				console.log('弹窗已关闭')
			},
			// 提交方法
			async submitForm(status) {
				this.orderData.workOrderId=this.workOrderId
				try {
					// 状态校验前置
					if (this.editStatus === 'FALSE') {
						return uni.showToast({
							title: '当前处于查看状态',
							icon: 'none'
						});
					}
					// 统一处理提交逻辑
					const isEditMode = this.editStatus === 'EDIT';
					const apiMethod = isEditMode ? foodProductionUpdate : foodProductionAdd;
					// 构造请求参数（避免污染原始数据）
					const payload = {
						...this.orderData,
						auditStatus: status,
					};

					// 显示加载状态
					uni.showLoading({
						title: '提交中...',
						mask: true
					});

				console.log(payload)
					// 执行API调用
					const res = await apiMethod(payload);

					if (res.code !== 0) {
						throw new Error(res.msg || '服务器处理异常');
					}

					// 统一处理成功逻辑
					uni.showToast({
						title: '提交成功',
						icon: 'success'
					});

					// 关闭当前页面，跳转到工单页
					this.$closeAndRedirect({
					  url: '/pages/work/history/index'
					});
				} catch (error) {
					// 统一错误处理
					console.error('提交失败:', error);
					uni.showToast({
						title: error.msg || '请求失败，请检查网络',
						icon: 'none',
						duration: 3000
					});
				} finally {
					uni.hideLoading();
				}
			},


			//获取字典值
			async getMedicalDict(type) {
				try {
					const res = await getMedicalDict(type)
					// 增加响应状态判断
					if (res.code === 0) {
						if (type === 'bid_type') {
							this.valueOptions = res.data.map(item => ({
								text: item.label,
								value: String(item.value) // 确保字典值转为字符串
							}));

						}
					} else {
						throw new Error(res?.data?.msg || '接口返回异常')
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '获取失败',
						icon: 'none',
						duration: 2000
					})
				}
			},
			// 获取工单详情
			async getfoodProductionInfo(orderId) {
				const id = {
					workOrderId: orderId
				}
				const res = await foodProductionInfo(id);
				if (res.code === 0) {
					this.orderData= res.data;
				}
			},

		}
	}
</script>

<style>
	/* 网页容器样式 */
	.page {
		flex: 1;
		background-color: #e9f1fe;
		padding: 20px;
		box-sizing: border-box;
	}


	/* 按钮容器样式 */
	.button-container {


		left: 0;
		right: 0;
		padding: 15px 20px;
		background: #ffffff;
		border-radius: 4px;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);

	}


	.uni-column {
		background-color: #e9f1fe;
		  height: auto;  /* 改为自动高度 */
		  min-height: 200px;  /* 设置最小高度 */
	}
	/* 新增多行输入框样式 */
	.multi-line-input {
	  width: 100%;
	  min-height: 150px;
	  padding: 12px;
	  font-size: 17px;
	  line-height: 1.5;
	  border-radius: 8px;
	  box-sizing: border-box;
	  transition: border-color 0.3s;
	}
	.multi-line-input:focus {
	  border-color: #007AFF;
	  outline: none;
	}
	
	.placeholder-style {
	  color: #999;
	  font-size: 17px;
	}
	.button-container {
			display: flex;
			background-color: #ffffff;
			width: 100%;
			padding: 10px;
			flex-direction: column;
			/* 垂直排列 */
	
			.button-save {
				margin-bottom: 10px;
			}
	
			.button-child {
				display: flex;
				flex-direction: row;
				/* 垂直排列 */
	
			}
		}

</style>